#author wendyeq@gmail.com

from __future__ import division#must be top
def rgb_to_hsv(r, g, b):
    maxc = max(r, g, b)
    minc = min(r, g, b)
    v = maxc/255
    if minc == maxc: return 0.0, 0.0, v
    s = (maxc-minc) / maxc
    rc = (maxc-r) / (maxc-minc)
    gc = (maxc-g) / (maxc-minc)
    bc = (maxc-b) / (maxc-minc)
    if r == maxc: h = bc-gc
    elif g == maxc: h = 2.0+rc-bc
    else: h = 4.0+gc-rc
    #h = (h/6.0) % 1.0
    h = h*60.0
    if h < 0.0: h = h+360.0
    return h, s, v

def hsv_vector(h,s,v):
    if h>315 or h<=20: h = 0
    elif h>20 and h<=40: h = 1
    elif h>40 and h<=75: h = 2
    elif h>75 and h<=155: h = 3
    elif h>155 and h<=190: h = 4
    elif h>190 and h<=270: h = 5
    elif h>270 and h<=295: h = 6
    elif h>296 and h<=315: h = 7

    if s>0 and s<=0.2: s=0
    elif s>0.2 and s<=0.7: s=1
    elif s>0.7 and s<=1: s=2

    if v>0 and v<=0.2: v=0
    elif v>0.2 and v<=0.7: v=1
    elif v>0.7 and v<=1: v=2
    return int(9*h+3*s+v)
